<?php

namespace Common\Model;

use Think\Model;

/**
 * 分类模型
 */
class ClassModel extends Model {

    const NORMAL = 0;   // 分类状态：正常
    const DELTEL = -1;  // 分类状态：删除（禁用）

    const CLASS_KEY = 'class:';     // 分类详细信息的缓存键名
    const CLASS_LIST_KEY = 'classlist:'; // 父类下的分类ID列表数据缓存键名
    const INDEX_DOC_CLASS_KEY = 'indexdocclass';  // 首页文档分类数据缓存键名
    const INDEX_BBS_CLASS_KEY = 'indexbbsclass';   // 首页社区分类数据缓存键名
    const CREATE_CLASS_KEY = 'createclass';  // 新建团体页面分类数据缓存键名

    /**
     * 根据分类ID获取分类详细信息
     * @param $classId
     * @return mixed
     */
    public static function getClassById($classId){
        if (!$classId) return null;
        $key = self::CLASS_KEY.$classId;
        $result = S($key);
        if (!$result && $result = M('Class')->field('pid,name,desc,status')->find($classId)) {
            S($key, $result);
        }
        return $result;
    }

    /**
     * 根据分类ID获取该分类下处于正常状态的子类id数组，如：[20,21,22,23]
     * @param $classId
     * @return mixed|string
     */
    public static function getClassSons($classId){
        if (!$classId) return null;
        $key = self::CLASS_LIST_KEY.$classId;
        $result = S($key);
        if(!$result){
            $where = [
                'pid' => $classId,
                'status' => self::NORMAL
            ];
            if($list = M('Class')->where($where)->field('id')->select()){
                $arr = [];
                foreach($list as $v){
                    $arr[] = $v['id'];
                }
                S($key, $arr);
                return $arr;
            }
        }
        return $result;
    }

    /**
     * 获取首页DOC模块的分类部分的代码
     * @return mixed|string
     */
    public static function getIndexDocClass(){
        $key = self::INDEX_DOC_CLASS_KEY;
        $result = S($key);
        if($result){
            return $result;
        }
        $html = '';
        $parentWhere = [
            'pid' => 0,
            'status' => self::NORMAL
        ];
        $parentClass = M('Class')->where($parentWhere)->field('id,name')->order('sort')->select();
        if(!$parentClass){
            return null;
        }
        foreach ($parentClass as $value){
            $html .= '<div class="head-nav-row"><a href="//doc.wooo.wang/c/'.$value['id'].'" class="fat" cid="'.$value['id'].'">'.$value['name'].'：</a><ul class="clearfix">';
            $childWhere = [
                'pid' => $value['id'],
                'status' => self::NORMAL
            ];
            $childClass = M('Class')->where($childWhere)->field('id,name')->order('sort')->select();
            if($childClass){
                foreach ($childClass as $v){
                    $html .= '<li><a href="//doc.wooo.wang/c/'.$v['id'].'" cid="'.$v['id'].'">'.$v['name'].'</a></li>';
                }
            }
            $html .= '</ul></div>';
        }
        S($key, $html);
        return $html;
    }

    /**
     * 获取首页BBS模块的分类部分的代码
     * @return mixed|string
     */
    public static function getIndexBbsClass(){
        $key = self::INDEX_BBS_CLASS_KEY;
        $result = S($key);
        if($result){
            return $result;
        }
        $html = '';
        $parentWhere = [
            'pid' => 0,
            'status' => self::NORMAL
        ];
        $parentClass = M('Class')->where($parentWhere)->field('id,name')->order('sort')->select();
        if(!$parentClass){
            return null;
        }
        foreach ($parentClass as $value){
            $html .= '<div class="head-nav-row"><a href="//bbs.wooo.wang/c/'.$value['id'].'" class="fat" cid="'.$value['id'].'">'.$value['name'].'：</a><ul class="clearfix">';
            $childWhere = [
                'pid' => $value['id'],
                'status' => self::NORMAL
            ];
            $childClass = M('Class')->where($childWhere)->field('id,name')->order('sort')->select();
            if($childClass){
                foreach ($childClass as $v){
                    $html .= '<li><a href="//bbs.wooo.wang/c/'.$v['id'].'" cid="'.$v['id'].'">'.$v['name'].'</a></li>';
                }
            }
            $html .= '</ul></div>';
        }
        S($key, $html);
        return $html;
    }

    /**
     * 获取新建团体页面的分类部分的代码
     * @return mixed|string
     */
    public static function getCreateClass(){
        $key = self::CREATE_CLASS_KEY;
        $result = S($key);
        if($result){
            return $result;
        }
        $html = '';
        $parentWhere = [
            'pid' => 0,
            'status' => self::NORMAL
        ];
        $parentClass = M('Class')->where($parentWhere)->field('id,name')->order('sort')->select();
        if(!$parentClass){
            return null;
        }
        foreach ($parentClass as $value){
            $html .= '<ul class="clearfix"><span>'.$value['name'].'：</span>';
            $childWhere = [
                'pid' => $value['id'],
                'status' => self::NORMAL
            ];
            $childClass = M('Class')->where($childWhere)->field('id,name')->order('sort')->select();
            if($childClass){
                foreach ($childClass as $v){
                    $html .= '<li><a href="javascript:vbid(0)" cid="'.$v['id'].'">'.$v['name'].'</a></li>';
                }
            }
            $html .= '</ul>';
        }
        S($key, $html);
        return $html;
    }

}